#include "../common/TreeNode.h"

/* 二叉搜索树插入结点 */
TreeNode* insert(TreeNode* root, int num) {
    if (root == nullptr) return nullptr;
    TreeNode *cur = root, *pre = nullptr;
    while (cur != nullptr) {
        if (cur->val == num) return nullptr;
        pre = cur;
        if (cur->val < num) cur = cur->right;
        else cur = cur->left;
    }
    TreeNode* node = new TreeNode(num);
    if (pre->val < num) pre->right = node;
    else pre->left = node;
    return node;
}
